﻿using UnityEngine;

namespace MFramework
{
    [System.Obsolete()]
    public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        private static Transform m_rootTF;

        public static T Inst
        {
            get
            {
                if(instance == null)
                {
                    instance = FindObjectOfType<T>();
                }
                if(instance == null)
                {
                    GameObject go = new GameObject(typeof(T).Name);
                    instance = go.AddComponent<T>();

                    if(m_rootTF == null)
                    {
                        m_rootTF = new GameObject("MonoSingletons").transform;
                        DontDestroyOnLoad(m_rootTF.gameObject);
                    }
                    go.transform.parent = m_rootTF;

                    instance.Init();
                }
                return instance;
            }
        }
        private static T instance;

        protected virtual void Init() { }
    }
}
